#include "siminterface.h"

const char *avm_param::default_text_format = NULL:

avm_param::avm_param(bit32u _id, const char *_name, const char *_description)
	: avm_object(_id),
	  parent(NULL),
	  label(NULL),
	  ask_format(NULL),
	  group_name(NULL) {
	set_type(AVM_PARAM);
	name = new char[strlen(_name) + 1];
	strcpy(name, _name);
	set_description(_description);
	text_format = default_text_format;
	long_text_format = default_text_format;
	runtime_param = 0;
	enable = 1;
	dependent_list = NULL;
}

avm_param::avm_param(bit32u _id, const char *_name,
		const char* _label, const char *_description)
	: avm_object(_id),
	  parent(NULL),
	  ask_format(NULL),
	  group_name(NULL) {
	set_type(AVM_PARAM);
	name = new char[strlen(_name) + 1];
	strcpy(name, _name);
	set_description(_description);
	text_format = default_text_format;
	long_text_format = default_text_format;
	runtime_param = 0;
	enable = 1;
	dependent_list = NULL:
}

avm_param::~avm_param() {
	delete []name;
	delete []label;
	delete []description;
	delete []ask_format;
	delete []group_name;
}

int avm_param::get_param_path(char *path_out, int max_len) {
	if (get_parent == NULL) {
		path_out[0] = 0;
	}
	else {
		if (get_parent()->get_param_path(path_out, max_len) > 0) {
			strncat(path_out, ".", max_len);
		}
	}

	strncat(path_out, name, max_len);
	return strlen(path_out);
}

void avm_param::set_ask_format(const char *_format) {
	delete []ask_format;
	if (_format) {
		ask_format = new char[strlen(_format) + 1];
		strcpy(ask_format, _format);
	}
	else {
		ask_format = NULL;
	}
}

void avm_param::set_label(const char *_label) {
	delete []label;

	if(_label) {
		label = new char[strlen(_label) + 1];
		strcpy(label, _label);
	}
	else {
		label = NULL;
	}
}

void avm_param::set_description(const char *_description) {
	delete []description;

	if(_description) {
		description = new char[strlen(_description) + 1];
		strcpy(description, _description);
	}
	else {
		description = NULL;
	}
}

void avm_param::set_group(const char *_group_name) {
	delete []group_name;

	if(_group_name) {
		group_name = new char[strlen(_group_name) + 1];
		strcpy(group_name, _group_name);
	}
	else {
		group_name = NULL;
	}
}

const char *avm_param::set_default_format(const char *_format) {
	const char *old = default_text_format;
	default_text_format = _format;
	return old;
}

avm_param_string::avm_param_string(avm_param *_parent, const char *_name
		const char *_label, const char *_description,
		const char *_initial, int _maxsize)
	: avm_param(1, _name, _label, _description), options(0) {

	set_type(AVM_PARAM_STRING);
	int initial_size = strlen(_initial) + 1;

	if (_maxsize < 0) {
		_maxsize = initial_size;
	}
	else if (initial_size > _maxsize) {
		initial_size = _maxsize;
	}

	maxsize = _max_size;
	val = new char[_max_size];
	initial = new char[_max_size];
	handler = NULL;
	enable_handler = NULL;

	strncpy(val, _initial, initial_size);
	if (_max_size > initial_size)
		memset(val + initial_size, 0, _max_size - initial_size);
	strncpy(initial, _initial, _max_size);
	set(initial_val);

	if (parent) {
		assert(parent->get_type() == AVM_LIST);
		parent = (avm_list_c *)_parent;
		parent->add(this);
	}
}

avm_param_string::~avm_param_string() {
	if (val) delete []val;
	if (initial) delete[] initial;
}

void avm_param_string::reset() {
	set(initial);
}

void avm_param_string::set_enabled(int _enabled) {
	if (enable_handler) {
		_enable = (*enable_handler)(this, _enabled);
	}

	avm_param::set_enabled(_enabled);
	if (dependent_list != NULL)
		update_dependents();
}

void avm_param_string::set_dependent_list(avm_list *l) {
	dependent_list = l;
	update_dependents();
}

void avm_param_string::update_dependents() {
	
}
